import axios from "axios";
import store from "../store";
import router from "../router";
import {Toast} from 'vant';
// 创建axios实例
const service = axios.create({
    baseURL: process.env.VUE_APP_URL, // api 的 VUE_APP_URL
    timeout: 50000, // 请求超时时间(因为需要调试后台,所以设置得比较大)
    // withCredentials: true
});

// request拦截器,在请求之前做一些处理
service.postJson = (params) => service({
    url: params.url,
    method: 'post',
    data: params.data,
    headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
})
let apiPath
service.interceptors.request.use(
    config => {
        let showLoading = true;
        apiPath=config.url
        
        if (config.params && config.params.silence) {
            delete config.params.silence;
            showLoading = false;
        }
        if (config.data && config.data.silence) {
            delete config.data.silence;
            showLoading = false;
        }

        if (config.url == "/WxOpen/GetSignature") {
            config.baseURL = '/wx/';
        }

        if (store.state.sessionId) {
            if (config.url.indexOf("?") == -1) { //如果URL包含？需要&连接token
                config.url = config.url + "?token=" + store.state.sessionId
            } else {
                config.url = config.url + "&token=" + store.state.sessionId
            }
        }


        if (showLoading) {
            Toast.loading({
                message: '加载中...',
                forbidClick: true,
            })
        }
        // // config.headers["Secure"] = true
        // if (config.headers['Content-Type'] === 'application/x-www-form-urlencoded') {
        //     config.headers['post']['Content-Type'] = 'application/x-www-form-urlencoded'
        //     config.headers['post']['Accept'] = 'application/x-www-form-urlencoded'

        // }
        // config.headers['post']['Content-Type'] = 'application/x-www-form-urlencoded'
        if (store.state.token) {
            // 给请求头添加laohu-token
            config.headers["user-token"] = store.state.token;
        }

        return config;
    },
    error => {
        console.log(error); // for debug
        Promise.reject(error);
    }
);

// response 拦截器,数据返回后进行一些处理

service.interceptors.response.use(
    response => {
        Toast.clear();
        /**
         * code为非20000是抛错 可结合自己业务进行修改
         */
        const res = response;
        if(apiPath=='/ai/GetDevice'||apiPath=='/ai/GetCameraPreviewURL'){
            return res.data
        }
        if (!res.data.success) {
            if (res.data.errorType === 3) {
                // to re-login
                router.push("/login");
                return
            }
            if (res.data.errorType === 1) {
                // to re-login
                Toast.fail({
                    mask: true,
                    message: res.data.errorText
                });
            }
            if (res.data.errorType === 11) {
                return res.data;
            }
            Toast.fail({
                mask: true,
                message: res.data.errorText
            });
            return Promise.reject(new Error(res.data.errorText || 'Error'))
        } else {
            return res.data
        }
        //   if (res.data.success == "0") {
        //     return res.data;
        //   } else if (res.data.code == "603") {
        //     // code为603代表token已经失效,
        //     // 提示用户,然后跳转到登陆页面
        //     router.push("/login");
        //   } else {
        //     Promise.reject(res.data.errorText);
        //   }
        //  return  Promise.resolve(res.data)
    },
    error => {
        return Promise.reject(new Error('网络异常'))
    }
);
export default service;